package com.example.finalwork2.component;

import com.example.finalwork2.entity.VehicleSignal;
import com.example.finalwork2.service.AlertService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "consumer_group_01")
public class RocketMQConsumer implements RocketMQListener<String> {

    @Autowired
    private AlertService alertService;

    private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

    @Override
    public void onMessage(String message) {
        VehicleSignal data = null;
        System.out.println("--------------------------------------------------------------------------------------------------------------");
        System.out.println("收到消息: " + message);


        try {
            //ObjectMapper mapper = new ObjectMapper();
            data = objectMapper.readValue(message, VehicleSignal.class);
            System.out.println("转换后的数据: " + data);
        } catch (Exception e) {
            System.err.println(" 在消费者处，消息反序列化失败: " + e.getMessage());
        }
        alertService.processSignalMessage(data);  // 传入对象，而不是字符串





    }
}

